home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectSound / Tutorials / Tut1 / frmCapture.frm < prev    next >
Text File  |  2001-10-08  |  8KB  |  246 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmCapture 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Audio Capture Tutorial"
  6.    ClientHeight    =   1395
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4305
  10.    Icon            =   "frmCapture.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   1395
  15.    ScaleWidth      =   4305
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin MSComDlg.CommonDialog cdlSave 
  18.       Left            =   4140
  19.       Top             =   0
  20.       _ExtentX        =   847
  21.       _ExtentY        =   847
  22.       _Version        =   393216
  23.       CancelError     =   -1  'True
  24.    End
  25.    Begin VB.CommandButton cmdSave 
  26.       Caption         =   "Sa&ve"
  27.       Enabled         =   0   'False
  28.       Height          =   375
  29.       Left            =   3038
  30.       TabIndex        =   2
  31.       Top             =   840
  32.       Width           =   975
  33.    End
  34.    Begin VB.CommandButton cmdStop 
  35.       Caption         =   "&Stop"
  36.       Enabled         =   0   'False
  37.       Height          =   375
  38.       Left            =   1898
  39.       TabIndex        =   1
  40.       Top             =   840
  41.       Width           =   975
  42.    End
  43.    Begin VB.CommandButton cmdStart 
  44.       Caption         =   "&Record"
  45.       Height          =   375
  46.       Left            =   638
  47.       TabIndex        =   0
  48.       Top             =   840
  49.       Width           =   975
  50.    End
  51.    Begin VB.Label lbl 
  52.       BackStyle       =   0  'Transparent
  53.       Caption         =   "Audio Capture Tutorial"
  54.       Height          =   255
  55.       Index           =   0
  56.       Left            =   660
  57.       TabIndex        =   4
  58.       Top             =   120
  59.       Width           =   2655
  60.    End
  61.    Begin VB.Label lbl 
  62.       BackStyle       =   0  'Transparent
  63.       Caption         =   "Copyright (C) 1999-2001 Microsoft Corporation, All Rights Reserved."
  64.       Height          =   435
  65.       Index           =   2
  66.       Left            =   660
  67.       TabIndex        =   3
  68.       Top             =   360
  69.       Width           =   3510
  70.    End
  71.    Begin VB.Image Image1 
  72.       Height          =   480
  73.       Left            =   120
  74.       Picture         =   "frmCapture.frx":0442
  75.       Top             =   120
  76.       Width           =   480
  77.    End
  78. End
  79. Attribute VB_Name = "frmCapture"
  80. Attribute VB_GlobalNameSpace = False
  81. Attribute VB_Creatable = False
  82. Attribute VB_PredeclaredId = True
  83. Attribute VB_Exposed = False
  84. Option Explicit
  85. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  86. '
  87. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  88. '
  89. '  File:       frmCapture.frm
  90. '
  91. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  92.  
  93. 'This tutorial will show basic functionality.  You will capture a buffer to memory,
  94. 'and then write it out to a file.
  95.  
  96. 'Variable declarations for our app
  97. Private dx As New DirectX8
  98. Private dsc As DirectSoundCapture8
  99. Private dscb As DirectSoundCaptureBuffer8
  100. Private dscd As DSCBUFFERDESC
  101. Private capFormat As WAVEFORMATEX
  102. Private ds As DirectSound8
  103.  
  104. Private Sub InitCapture()
  105.  
  106.     Dim cCaps As DSCCAPS
  107.     On Local Error Resume Next
  108.     'We need to create a direct sound object before the capture object
  109.     If ds Is Nothing Then Set ds = dx.DirectSoundCreate(vbNullString)
  110.     If Err Then
  111.         MsgBox "Unable to create a DirectSound object", vbOKOnly Or vbCritical, "Cannot continue"
  112.         Cleanup
  113.         End
  114.     End If
  115.     'First we need to create our capture buffer on the default object
  116.     Set dsc = dx.DirectSoundCaptureCreate(vbNullString)
  117.     If Err Then
  118.         MsgBox "Unable to create a Capture object", vbOKOnly Or vbCritical, "Cannot continue"
  119.         Cleanup
  120.         End
  121.     End If
  122.     
  123.     'Lets get the caps for our object
  124.     dsc.GetCaps cCaps
  125.     
  126.     'Check for a capture format we will support in the sample
  127.     If cCaps.lFormats And WAVE_FORMAT_4M08 Then
  128.         capFormat = CreateWaveFormatEx(44100, 1, 8)
  129.     ElseIf cCaps.lFormats And WAVE_FORMAT_2M08 Then
  130.         capFormat = CreateWaveFormatEx(22050, 1, 8)
  131.     ElseIf cCaps.lFormats And WAVE_FORMAT_1M08 Then
  132.         capFormat = CreateWaveFormatEx(11025, 1, 8)
  133.     Else
  134.         MsgBox "Could not get the caps we need on this card.", vbOKOnly Or vbCritical, "Exiting."
  135.         Cleanup
  136.         End
  137.     End If
  138.     
  139. End Sub
  140.  
  141. Private Sub CreateCaptureBuffer()
  142.     dscd.fxFormat = capFormat
  143.     dscd.lBufferBytes = capFormat.lAvgBytesPerSec * 20
  144.     dscd.lFlags = DSCBCAPS_WAVEMAPPED
  145.     
  146.     Set dscb = dsc.CreateCaptureBuffer(dscd)
  147. End Sub
  148. Private Sub Cleanup()
  149.     Set ds = Nothing
  150.     Set dscb = Nothing
  151.     Set dsc = Nothing
  152.     Set dx = Nothing
  153. End Sub
  154. Private Function CreateWaveFormatEx(Hz As Long, Channels As Integer, BITS As Integer) As WAVEFORMATEX
  155.  
  156.     'Create a WaveFormatEX structure using the vars we provide
  157.     With CreateWaveFormatEx
  158.         .nFormatTag = WAVE_FORMAT_PCM
  159.         .nChannels = Channels
  160.         .lSamplesPerSec = Hz
  161.         .nBitsPerSample = BITS
  162.         .nBlockAlign = Channels * BITS / 8
  163.         .lAvgBytesPerSec = .lSamplesPerSec * .nBlockAlign
  164.         .nSize = 0
  165.     End With
  166. End Function
  167.  
  168. Private Sub cmdSave_Click()
  169.     On Local Error Resume Next
  170.     With cdlSave
  171.         'Set our initial properties
  172.         .FileName = vbNullString
  173.         .flags = cdlOFNHideReadOnly
  174.         .Filter = "Wave files(*.WAV)|*.wav"
  175.         .ShowOpen
  176.         If Err Then Exit Sub 'We clicked cancel
  177.         If .FileName = vbNullString Then Exit Sub 'No file
  178.         'Save the file to disk
  179.         GetSoundBufferFromCapture(dscb).SaveToFile .FileName
  180.     End With
  181. End Sub
  182.  
  183. Private Sub cmdStart_Click()
  184.     'We want to record sound now.
  185.     
  186.     'First we need to get rid of any sound we may have
  187.     Set dscb = Nothing
  188.     'Now get our capture buffer once more
  189.     CreateCaptureBuffer
  190.     
  191.     'Now start recording
  192.     dscb.Start DSCBSTART_DEFAULT
  193.     'Disable/Enable our buttons accordingly
  194.     cmdStop.Enabled = True
  195.     cmdStart.Enabled = False
  196.     cmdSave.Enabled = False
  197. End Sub
  198.  
  199. Private Sub cmdStop_Click()
  200.     Dim lbufferStatus As Long
  201.     
  202.     'Stop the buffer
  203.     dscb.Stop
  204.     
  205.     'Disable/Enable our buttons accordingly
  206.     cmdStop.Enabled = False
  207.     cmdStart.Enabled = True
  208.     cmdSave.Enabled = True
  209. End Sub
  210.  
  211. Private Sub Form_Load()
  212.  
  213.     'Lets init our capture device
  214.     InitCapture
  215. End Sub
  216.  
  217. Private Sub Form_Unload(Cancel As Integer)
  218.     Cleanup
  219. End Sub
  220.  
  221. Private Function GetSoundBufferFromCapture(ByVal oCaptureBuffer As DirectSoundCaptureBuffer8) As DirectSoundSecondaryBuffer8
  222.     Dim lbufferStatus As Long
  223.     Dim capCURS As DSCURSORS
  224.     Dim dsd As DSBUFFERDESC
  225.     Dim ByteBuffer() As Integer 'Our digital data from our capture buffer
  226.     
  227.     'Are we still capturing? If so, stop
  228.     oCaptureBuffer.Stop
  229.     
  230.     'Get the capture info
  231.     oCaptureBuffer.GetCurrentPosition capCURS
  232.     dsd.lBufferBytes = capCURS.lWrite + 1
  233.     dsd.fxFormat = dscd.fxFormat
  234.     'If there is nothing to write, then exit
  235.     If capCURS.lWrite = 0 Then Exit Function
  236.     
  237.     Set GetSoundBufferFromCapture = ds.CreateSoundBuffer(dsd)
  238.     'Set the size for our new Data
  239.     ReDim ByteBuffer(capCURS.lWrite)
  240.     'Read the data from our capture buffer
  241.     oCaptureBuffer.ReadBuffer 0, capCURS.lWrite, ByteBuffer(0), DSCBLOCK_DEFAULT
  242.     'Write the data to our sound buffer
  243.     GetSoundBufferFromCapture.WriteBuffer 0, capCURS.lWrite, ByteBuffer(0), DSBLOCK_DEFAULT
  244.  
  245. End Function
  246.